<?php
/**
 * @title 328. 奇偶链表
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function oddEvenList($head) {
        /**
         * @since 2024-01-10 链表数量<=2
         */
        if($head === null OR $head->next === null OR $head->next->next === null){
            return $head;
        } else {}

        /**
         * @since 2024-01-10 奇数尾节点，偶数尾节点
         */
        $oddTail = $head;
        $evenTail = $head->next;

        /**
         * @since 2024-01-10 从第3个节点开始遍历
         */
        $i = 3;
        $node = $evenTail->next;
        $evenTail->next = null;
        while($node !== null){
            /**
             * @since 2024-01-10 下个节点
             */
            $nextNode = $node->next;

            /**
             * @since 2024-01-10 奇数
             */
            if($i%2 == 1) {
                /**
                 * @since 2024-01-10 node节点插入到奇数尾节点后面
                 */
                $oddTailNext = $oddTail->next;
                $oddTail->next = $node;
                $node->next = $oddTailNext;

                /**
                 * @since 2024-01-10 新的奇数尾节点
                 */
                $oddTail = $node;
            } elseif($i%2 == 0) {
                /**
                 * @since 2024-01-10 node节点插入到偶数尾节点后面
                 */
                $evenTail->next = $node;
                $node->next = null;

                /**
                 * @since 2024-01-10 新的偶数尾节点
                 */
                $evenTail = $node;
            } else {}

            /**
             * @since 2024-01-10 遍历下一个节点
             */
            $node = $nextNode;
            $i++;
        }

        /**
         * @return ListNode
         */
        return $head;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.10_7.php
 */
$datas = [
    [1,2,3,4,5],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    $result = $obj->oddEvenList($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
